<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        canvas {
            border: 1px solid red;
        }
    </style>
</head>
<body>
    <canvas width="500" height="500" id="mycanvas"></canvas>
    <script>
        let canvas = document.querySelector("#mycanvas");
        let context = canvas.getContext("2d");

        // 预加载，等资源加载完毕后再使用
        function loadImg() {
            let img = document.createElement('img')
            img.src = 'https://img2.baidu.com/it/u=1814268193,3619863984&fm=253&fmt=auto&app=138&f=JPEG?w=632&h=500'
            context.drawImage(img, 50, 50, 300, 300)
        }
        loadImg()




    </script>
</body>
</html>